package com.hns.can.service.service;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.entity.BaseDrvChkParamInfoEntity;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataUtil;
import com.hns.tool.pub.PubUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 考核权重设置Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class DriverTotalScoreService extends CommonService {

    /**
     * 驾驶员总评分设置 - 获取该组织机构下的驾驶员总评分设置信息，如果没有的话显示为默认的信息
     * @param paraMap
     * @author:zhangzq
     * @email:
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<BaseDrvChkParamInfoEntity> searchDriverTotalScoreInfo(Map<String, Object> paraMap) {
        List<BaseDrvChkParamInfoEntity> list = PubUtil.convertListMapToObject(
                this.sqlMapDao.selectList("baseDrvChkParamInfo.searchBaseDrvChkParamInfo", paraMap), BaseDrvChkParamInfoEntity.class);
        return list;
    }

    /**
     * 新建或更新驾驶员总评分设置
     * @param seleDrvOrganId
     * @param currDrvOrganId
     * @param chkIds
     * @param weightValues
     * @param corpId
     * @author:zhangzq
     * @email:
     */
    @Transactional
    public void uptDriverTotalScoreInfo(String seleDrvOrganId,String currDrvOrganId, String[] chkIds, BigDecimal[] weightValues,String corpId) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        this.getCurrentUser().getUser();
        if(seleDrvOrganId.equals(currDrvOrganId)){ //更新已有的数据
            for(int i = 0; i < chkIds.length; i++){
                BaseDrvChkParamInfoEntity entity = this.commonEntityDao.find(chkIds[i], BaseDrvChkParamInfoEntity.class);
                entity.setWeightValue(weightValues[i]);
                entity.setUpdateTime(new Date());
                entity.setUpdateUser(user.getAcctName());
                this.commonEntityDao.update(entity);
            }
        }else{ //添加新数据
            for(int i = 0; i < chkIds.length; i++){
                BaseDrvChkParamInfoEntity entity = this.commonEntityDao.find(chkIds[i], BaseDrvChkParamInfoEntity.class);
                BaseDrvChkParamInfoEntity dbentity = new BaseDrvChkParamInfoEntity();
                dbentity.setChkId(DataUtil.sequenceID());
                PubUtil.copyPropertiesInclude(entity, dbentity, new String[]{"organLineType","chkCgyCode","chkCgyName","chkCode","chkName","param","deductValue","allowUnit","allowCount"});
                dbentity.setOrganLineId(seleDrvOrganId);
                dbentity.setWeightValue(weightValues[i]);
                dbentity.setCrtTime(new Date());
                dbentity.setCrtUser(user.getAcctName());
                dbentity.setUpdateTime(new Date());
                dbentity.setUpdateUser(user.getAcctName());
                dbentity.setCorpId(corpId);
                this.commonEntityDao.save(dbentity);
            }
        }
    }

}
